package com.exartisan.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.exartisan.bean.Event;
import com.exartisan.bean.EventDetail;
import com.exartisan.bean.FbComment;
import com.exartisan.network.NetworkUtility;
import com.exartisan.thanhdoanhn.PhotoActivity;
import com.exartisan.thanhdoanhn.R;
import com.exartisan.util.DataHolder;
import com.exartisan.util.DateTimeFormat;
import com.exartisan.util.Util;
import com.loopj.android.image.SmartImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class EventDetailAdapter extends BaseAdapter {
	EventDetail detail;
	Event event;
	ArrayList<FbComment> fbComments;
	private Context ctx;
	boolean isMoreDetail;
	String wvURL;

	public EventDetailAdapter(EventDetail detail,
			ArrayList<FbComment> fbComments, Context ctx, boolean isMoreDetail,
			String wvURL) {
		this.detail = detail;
		this.fbComments = fbComments;
		this.ctx = ctx;
		this.isMoreDetail = isMoreDetail;
		this.wvURL = wvURL;
	}

	public EventDetailAdapter(Event event, Context ctx, boolean isMoreDetail,
			String wvURL) {
		this.event = event;
		this.fbComments = new ArrayList<FbComment>();
		this.ctx = ctx;
		this.isMoreDetail = isMoreDetail;
		this.wvURL = wvURL;
	}

	@Override
	public int getCount() {
		if (isMoreDetail) {
			return fbComments.size() + 2;
		} else
			return fbComments.size() + 1;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		LayoutInflater inflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View listView;

		listView = new View(ctx);
		switch (getItemViewType(position)) {
		case 0:
			if (convertView == null) {
				// declare
				listView = inflater.inflate(R.layout.event_detail_row1, null);
				ImageView  imgCover = (ImageView) listView
						.findViewById(R.id.imgCover);
				TextView tvTitle = (TextView) listView
						.findViewById(R.id.tv_title);
				TextView tvDesc = (TextView) listView
						.findViewById(R.id.tv_description);
				TextView tvTimeStart = (TextView) listView
						.findViewById(R.id.tvTimeStart);
				TextView tvTimeEnd = (TextView) listView
						.findViewById(R.id.tvTimeEnd);
				TextView tvPlace = (TextView) listView
						.findViewById(R.id.tv_place);
				TextView tvCountLike = (TextView) listView
						.findViewById(R.id.tv_CountLike);
				TextView tvCountComment = (TextView) listView
						.findViewById(R.id.tv_CountComment);
				RelativeLayout Re_Gallery = (RelativeLayout) listView
						.findViewById(R.id.Re_Gallery);

				// fill data
				/*
				 * imgAvatar .setImageUrl(
				 * "http://img3.tamtay.vn/files/photo2/2013/9/20/21/11519159/523c5804_764e8dd9__ken2060.jpg"
				 * ); tvTitle.setText(detail.getTitle());
				 * tvDesc.setText(detail.getDesc());
				 * tvTimeStart.setText(detail.getTimeStart());
				 * tvTimeEnd.setText(detail.getTimeEnd());
				 * tvPlace.setText(detail.getPlace());
				 * tvCountLike.setText(detail.getCountLike());
				 * tvCountComment.setText(detail.getCountLike());
				 */

				//imgAvatar.setImageUrl(event.getCoverUrl());
				
				ImageLoader.getInstance().displayImage(event.getCoverUrl(), imgCover);
				tvTitle.setText(event.getEventName());
				tvDesc.setText(event.getDescription());
				tvTimeStart.setText(DateTimeFormat.format(event.getStartDate(), DateTimeFormat.SHORT_VALUE));
				tvTimeEnd.setText(DateTimeFormat.format(event.getEndDate(), DateTimeFormat.SHORT_VALUE));
				tvPlace.setText(event.getPlace());
				Util.setLikeCount(event.getFbFeedID(), tvCountLike);
				Util.setCommentCount(event.getFbFeedID(), tvCountComment);
				Re_Gallery.setOnClickListener(new OnClickListener()
				{

					@Override
					public void onClick(View v)
					{
						Intent intent = new Intent(ctx, PhotoActivity.class);
						intent.putExtra("type", 1);
						intent.putExtra("id", event.getEventID());
						Log.d("detailevent", ""+DataHolder.getEvent().getEventID() );
						ctx.startActivity(intent);
					}
				});
			} else {
				listView = (View) convertView;
				TextView tvCountLike = (TextView) listView
						.findViewById(R.id.tv_CountLike);
				TextView tvCountComment = (TextView) listView
						.findViewById(R.id.tv_CountComment);
				Util.setLikeCount(event.getFbFeedID(), tvCountLike);
				Util.setCommentCount(event.getFbFeedID(), tvCountComment);
			}
			break;
		case 1:
			if (convertView == null) {
				// declare
				listView = inflater.inflate(R.layout.event_detail_row_webview,
						null);
				final WebView webView = (WebView) listView
						.findViewById(R.id.wbv_contentEvent);
				WebSettings webSettings = webView.getSettings();
				webSettings.setJavaScriptEnabled(true);
				webView.loadUrl(wvURL);
				
				final ProgressBar prBar = (ProgressBar) listView.findViewById(R.id.pro_webevent);
				final LinearLayout l = (LinearLayout) listView.findViewById(R.id.linner_wv);
				
				webView.setWebViewClient(new WebViewClient(){
					@Override
					public void onPageFinished(WebView view, String url) {
						super.onPageFinished(view, url);
						l.setVisibility(View.VISIBLE);
						prBar.setVisibility(View.GONE);
					}
				});
				
			} else {
				listView = (View) convertView;
			}
			break;
		default:
			CommentViewHolder holder;
			if (convertView == null) {
				holder = new CommentViewHolder();
				listView = inflater.inflate(R.layout.event_detail_comments,
						null);
				holder.imgAvatar = (SmartImageView) listView
						.findViewById(R.id.img_avataComment);
				holder.tvUserName = (TextView) listView
						.findViewById(R.id.tv_userNameComment);
				holder.tvCreatedDate = (TextView) listView
						.findViewById(R.id.tv_dateComment);
				holder.tvComment = (TextView) listView
						.findViewById(R.id.tv_commentForum);
				listView.setTag(holder);
			} else {
				listView = (View) convertView;
				holder = (CommentViewHolder) listView.getTag();
			}
			FbComment comment;
			if (isMoreDetail)   comment = fbComments.get(position - 2);
			else comment = fbComments.get(position - 1);
			holder.tvUserName.setText(comment.getFbUser().getName());
			holder.tvCreatedDate.setText(DateTimeFormat.format(comment.getCreated_time(), DateTimeFormat.SHORT_VALUE));
			holder.tvComment.setText(comment.getMessage());
			ImageLoader.getInstance().displayImage(NetworkUtility.GRAPH_API_URL + "/" + comment.getFbUser().getId() + "/picture?width=100&height=100", holder.imgAvatar);
			
			//holder.imgAvatar.setImageUrl(NetworkUtility.GRAPH_API_URL + "/" + comment.getFbUser().getId() + "/picture?width=100&height=100");
			
			break;
		}

		return listView;
	}

	@Override
	public int getViewTypeCount() {
		return 3;
	}

	@Override
	public int getItemViewType(int position) {

		if (position == 0)
			return 0;
		else if (position == 1 && isMoreDetail == true)
			return 1;
		else
			return 2;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}
	
	public void setComments(ArrayList<FbComment> comments){
		this.fbComments = comments;
	}
	
	private class CommentViewHolder{
		SmartImageView imgAvatar;
		TextView tvUserName, tvCreatedDate, tvComment;
	}

}
